#pragma once

#include <iostream>
#include <string>

#define Die(code) do{exit(code);} while(0)

#define CONV(v) (struct sockaddr*)(v)

enum
{
    USAGE_ERR = 1,
    SOCKET_ERR,
    BIND_ERR,
    LISTEN_ERR
};


const int gdefaultsockfd = -1;
const int gbacklog = 8;

// 1. 正常字符串 2. out 空串 && 返回值是 true  3. out 空串 && 返回值位 false
bool ParseOneLine(std::string &str, std::string *out, const std::string &sep)
{
    auto pos = str.find(sep); // 找分隔符
    if(pos == std::string::npos) return false;
    *out = str.substr(0, pos); // [0, pos)
    str.erase(0, pos + sep.size()); // 清空
    return true;
}

// Connection: keep-alive
bool SplitString(const std::string &header, const std::string &sep, std::string *key, std::string *value)
{
    auto pos = header.find(sep);
    if(pos == std::string::npos) return false;

    *key = header.substr(0, pos);
    *value = header.substr(pos + sep.size());
    
    return true;
}